<?php

class admin_Model_ContactoMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {

        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Enlace a tabla de datos inválido');
        }

        $this->_dbTable = $dbTable;

        return $this;
    }

    public function getDbTable() {

        if (null === $this->_dbTable) {
            $this->setDbTable('admin_Model_DbTable_Contacto');
        }

        return $this->_dbTable;
    }

     public function obtenerTodos() {

        $select = $this->getDbTable()->select();
        $select->order('fecha desc');
        return $this->getDbTable()->fetchAll($select);

    }

    public function obtenerPorId($id) {

        $id = (int) $id;
        return $this->getDbTable()->find($id)->current();

    }

    public function agregar(admin_Model_Contacto $objContacto) {

        $contacto = $this->getDbTable()->createRow();
        $contacto->asunto = $objContacto->getAsunto();
        $contacto->nombre = $objContacto->getNombre();
        $contacto->email = $objContacto->getEmail();
        $contacto->empresa = $objContacto->getEmpresa();
        $contacto->pais = $objContacto->getPais();
        $contacto->telefono = $objContacto->getTelefono();
        $contacto->mensaje = $objContacto->getMensaje();
        $contacto->fecha = $objContacto->getFecha();
        $contacto->ip = $objContacto->getIp();

        $contacto->save();
    }

    public function editar(admin_Model_Contacto $objContacto) {

        $contacto = array(
            'asunto' => $objContacto->getAsunto(),
            'nombre' => $objContacto->getNombre(),
            'email' => $objContacto->getEmail(),
            'empresa' => $objContacto->getEmpresa(),
            'pais' => $objContacto->getPais(),
            'telefono' => $objContacto->getTelefono(),
            'mensaje' => $objContacto->getMensaje(),
            'fecha' => $objContacto->getContacto()
        );

        $this->getDbTable()->update($contacto, 'id=' . (int) $objContacto->getId());
    }

    public function eliminar($id) {

        $id = (int) $id;
        $contacto = $this->getDbTable()->find($id)->current();

        if ($contacto !== null) {
            $contacto->delete();
        }
    }




}

